//
//  SecondViewController.m
//  XDCommonApp
//
//  Created by XD-XY on 2/12/14.
//  Copyright (c) 2014 XD-XY. All rights reserved.
//  

#import "SecondViewController.h"
#import "XDHeader.h"
#import "XDHeader.h"
#import "XDTools.h"
#import "ArtistCell.h"
#import "XDSearchView.h"

#import "ArtistItem.h"
#import "GalleryItem.h"
#import "UIImageView+WebCache.h"
#import "SearchViewController.h"
#import "SearchItem.h"
#import "SearchCell.h"
#import "WaterDetailViewController.h"
#import "MostDetailViewController.h"
#define btn_tag 10000
#define TabViewheight 60
@interface SecondViewController ()

@end

@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    DDLOG_CURRENT_METHOD;
    [super viewWillAppear:animated];
//    [_searchDataArray removeAllObjects];
//    [_searchTableView reloadData];
//    [searchView.mytextField resignFirstResponder];
//    searchView.mytextField.text = nil;
//    _searchBgview.hidden=YES;
    NSString * str = [[NSUserDefaults standardUserDefaults] objectForKey:@"SecondViewHiddenSearch"];
    if ([str isEqualToString:@"yes"]){
        [searchView clearClick];
    }else{

    }
}




- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.

    
    self.leftBtn.hidden =YES;
    self.titleLabel.text = SECONDVIEWTITLETEXT;
    
    self.artistArray = [[NSMutableArray alloc] init];
    self.galleryArray = [[NSMutableArray alloc] init];
    self.searchDataArray = [[NSMutableArray alloc] init];
    isArtist = YES;
    artistTotalCount = 0;
    galleryTotalCount = 0;
    isHeaderRefresh = YES;
    page = 0;
    page2 =0;
    isHaveMoreData =YES;
    isHaveMoreData2 = YES;
    hiddenSarch =YES;
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,UI_SCREEN_WIDTH, self.contentView.frame.size.height-UI_TAB_BAR_HEIGHT) style:UITableViewStylePlain];
    tableView.delegate =self;
    tableView.dataSource = self;
    [self.contentView addSubview:tableView];
    //艺术家tabbar
    artistBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    artistBtn.frame =CGRectMake(0,0,UI_SCREEN_WIDTH/2, UI_NAVIGATION_BAR_HEIGHT);
    artistBtn.backgroundColor = [UIColor clearColor];
    [artistBtn setTitle:@"艺术家" forState:UIControlStateNormal];
    artistBtn.titleLabel.font = NAVIGATIONTITLEFONT;
    [artistBtn setTitleColor:TEXTBLACKCOLORE3 forState:UIControlStateNormal];
    [artistBtn setTitleColor:TEXTBLACKCOLORE forState:UIControlStateDisabled];
    [artistBtn setTitleColor:TEXTBLACKCOLORE forState:UIControlStateHighlighted];
    [artistBtn setAdjustsImageWhenHighlighted:NO];
    [artistBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
    artistBtn.enabled =NO;
    artistBtn.tag =btn_tag;
    [self.navigationBarView addSubview:artistBtn];
    
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(width_x(artistBtn), 0, 1, UI_NAVIGATION_BAR_HEIGHT)];
    lineView.backgroundColor = RGBA(219, 219, 219, 1);
    [self.navigationBarView addSubview:lineView];
    
    //画廊tabbar
    galleryBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    galleryBtn.frame =CGRectMake(width_x(artistBtn)+1,0,UI_SCREEN_WIDTH/2, UI_NAVIGATION_BAR_HEIGHT-1);
    galleryBtn.backgroundColor = [UIColor clearColor];
    galleryBtn.titleLabel.font = NAVIGATIONTITLEFONT;
    [galleryBtn setTitle:@"画廊" forState:UIControlStateNormal];
    [galleryBtn setTitleColor:TEXTBLACKCOLORE3 forState:UIControlStateNormal];
    [galleryBtn setTitleColor:TEXTBLACKCOLORE forState:UIControlStateDisabled];
    [galleryBtn setTitleColor:TEXTBLACKCOLORE forState:UIControlStateHighlighted];
    [galleryBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
    [galleryBtn setAdjustsImageWhenHighlighted:NO];
    galleryBtn.enabled =YES;
    galleryBtn.tag =btn_tag+1;
    [self.navigationBarView addSubview:galleryBtn];
    
    [self createArtistView];
    [self createGalleryView];
    [self initSearchTableView];
    [self getArtistData];
    [self getGalleryData];
}

#pragma mark  ========================create UI==========================
-(void)createArtistView
{
    //搜索
    searchView = [[XDSearchView alloc] initWithFrame:CGRectMake(0,0, UI_SCREEN_WIDTH, 97/2.0f) ];
    searchView.backgroundColor = RGBA(204, 204, 204, 1);
    searchView.delegate=self;
    [self.contentView addSubview:searchView];
    
    //艺术家列表
    self.artistTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,height_y(searchView), UI_SCREEN_WIDTH, self.contentView.frame.size.height-UI_TAB_BAR_HEIGHT-searchView.frame.size.height) style:UITableViewStylePlain];
    _artistTableView.delegate = self;
    _artistTableView.dataSource = self;
    [self.contentView addSubview:_artistTableView];
    
    header = [[MJRefreshHeaderView alloc] init];
    header.delegate = self;
    header.scrollView = _artistTableView;
    
    footer = [[MJRefreshFooterView alloc] init];
    footer.delegate = self;
    footer.scrollView = _artistTableView;
}

-(void)initSearchTableView
{
    self.searchBgview = [[UIView alloc] initWithFrame:CGRectMake(0,height_y(searchView),UI_SCREEN_WIDTH, self.contentView.frame.size.height-searchView.frame.size.height)];
    _searchBgview.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_searchBgview];
    
    self.searchTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, UI_SCREEN_WIDTH, self.contentView.frame.size.height-252-searchView.frame.size.height) style:UITableViewStylePlain];
    _searchTableView.backgroundColor = [UIColor whiteColor];
    _searchTableView.dataSource = self;
    _searchTableView.delegate =self;
    [_searchBgview addSubview:_searchTableView];
    _searchBgview.hidden =YES;
}

-(void)createGalleryView
{
    
}

#pragma mark ========================MJReflash delegate========================
-(void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if (refreshView == header){
        isHeaderRefresh = YES;
    }else{
        isHeaderRefresh = NO;
    }
    
    if (isArtist){
        [self getArtistData];
    }else{
        [self getGalleryData];
    }
}

#pragma mark  ========================UIButton 点击事件==========================
-(void)btnClick:(UIButton * )sender
{
    artistBtn.enabled = sender.tag==btn_tag?NO:YES;
    galleryBtn.enabled = sender.tag==btn_tag+1?NO:YES;
    if (!artistBtn.enabled){
        isArtist =YES;
        searchView.mytextField.placeholder = @"搜索作品或艺术家";
    }else{
        searchView.mytextField.placeholder = @"搜索作品或画廊";
        isArtist =NO;
    }
    [_artistTableView reloadData];
    
    [searchView clearClick];
}

#pragma mark  ========================XDSearchView delegate==========================

#pragma mark ---------------XDSearchView Delegate----------
-(void)XDSearhViewShouldBeginEditing
{
    DDLOG_CURRENT_METHOD;
   // [_searchDataArray removeAllObjects];
   // [_searchTableView reloadData];
    _searchBgview.hidden =NO;
}

-(void)XDSearchViewShouldReturn
{
    DDLOG_CURRENT_METHOD;
    _searchTableView.frame = _searchBgview.bounds;
}

-(void)XDSearchViewCanelbuttonClick
{
    DDLOG_CURRENT_METHOD;
    searchView.mytextField.text = nil;
    [_searchDataArray removeAllObjects];
    [_searchTableView reloadData];
    _searchBgview.hidden = YES;
}

-(void)XDSearchTextFieldDidChange
{
    DDLOG_CURRENT_METHOD;
    DDLOG(@"searchView.mytextfield.text = %@",searchView.mytextField.text);
    [self getSearchData:searchView.mytextField.text];
}



#pragma mark  ========================UITableView delegate==========================
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _searchTableView){
        return 60;
    }
    return 101/2.0f;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _searchTableView){
        return [_searchDataArray count];
    }else{
        if (isArtist){
            return [_artistArray count];
        }else{
            return [_galleryArray count];
        }
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == _searchTableView){
        static NSString * cellName = @"cell2";
        SearchCell * cell = [tableView dequeueReusableCellWithIdentifier:cellName];
        if (cell == nil){
            cell = [[SearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
            cell.backgroundColor = [UIColor whiteColor];
        }
        if ([_searchDataArray count]==0){
            return cell;
        }
        SearchItem * item = [_searchDataArray objectAtIndex:indexPath.row];
        cell.headerIamgeView.image = [UIImage imageNamed:@"search_artist_portal"];
        cell.titleLabel.text = [NSString stringWithFormat:@"%@   %@",item.ArtistName,item.ArtworkName];
        return cell;
    }else{
        static NSString * cellName = @"cell";
        ArtistCell * cell = [tableView dequeueReusableCellWithIdentifier:cellName];
        if (cell == nil){
            cell = [[ArtistCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
        }
        cell.headImageView.image = nil;
        cell.nameLabel.text= nil;
        cell.numbersLabel.text = nil;
        cell.browseLabel.text =nil;
        if (isArtist){
            if ([_artistArray count]==0){
                return cell;
            }
            ArtistItem * item = [_artistArray objectAtIndex:indexPath.row];
            NSString * iamgeUrl = [NSString stringWithFormat:@"%@%@",ImageMainUrl,item.artistPortal];
            NSString * urlString=[iamgeUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [cell.headImageView setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"11"]];
            //        DDLOG(@"urlString = %@",urlString);
            //        cell.headImageView.image = [UIImage imageNamed:@"66"];
            cell.nameLabel.text = item.artistName;
            if ([item.artistWorkCount intValue]>0){
                cell.numbersLabel.text = [NSString stringWithFormat:@"(%@件作品)",item.artistWorkCount];
            }else{
                cell.numbersLabel.text = nil;
            }
            cell.browseLabel.text = item.artistOpenTime;
            CGRect rect = [XDTools xyAutoSizeOfWidget:cell.nameLabel andSize:CGSizeMake(100, 1000) andtextFont:15];
            cell.nameLabel.frame = CGRectMake(129/2.0f, 37/2.0f, rect.size.width, rect.size.height);
            cell.numbersLabel.frame =CGRectMake(width_x(cell.nameLabel)+23/2.0f, 40/2.0f, 130, 12.5);
            
            CGRect rect2 = [XDTools xyAutoSizeOfWidget:cell.browseLabel andSize:CGSizeMake(100, 1000) andtextFont:12];
            cell.browseLabel.frame = CGRectMake(UI_SCREEN_WIDTH-rect2.size.width-15, rect2.origin.y, rect2.size.width,101/2.0f);
            cell.rightImageView.frame = CGRectMake(cell.browseLabel.frame.origin.x-6-12,42/2.0f, 24/2, 16/2);
            
        }else{
            if ([_galleryArray count]==0){
                return cell;
            }
            GalleryItem * item = [_galleryArray objectAtIndex:indexPath.row];
            NSString * iamgeUrl = [NSString stringWithFormat:@"%@%@",ImageMainUrl,item.logoUrl];
            NSString * urlString=[iamgeUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [cell.headImageView setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"77"]];
            DDLOG(@"urlString = %@",urlString);
            //        cell.headImageView.image = [UIImage imageNamed:@"66"];
            cell.nameLabel.text = item.name;
            if ([item.workCount intValue]>0){
                cell.numbersLabel.text = [NSString stringWithFormat:@"(%@件作品)",item.workCount];
            }else{
                cell.numbersLabel.text = nil;
            }
            cell.browseLabel.text = item.openTimes;
            
            CGRect rect = [XDTools xyAutoSizeOfWidget:cell.nameLabel andSize:CGSizeMake(120, 30) andtextFont:15];
            cell.nameLabel.frame = CGRectMake(129/2.0f, 37/2.0f, rect.size.width, rect.size.height);
            
            cell.numbersLabel.frame =CGRectMake(width_x(cell.nameLabel)+23/2.0f, 40/2.0f, 130, 12.5);
            
            CGRect rect2 = [XDTools xyAutoSizeOfWidget:cell.browseLabel andSize:CGSizeMake(100, 1000) andtextFont:12];
            cell.browseLabel.frame = CGRectMake(UI_SCREEN_WIDTH-rect2.size.width-15, rect2.origin.y, rect2.size.width,101/2.0f);
            cell.rightImageView.frame = CGRectMake(cell.browseLabel.frame.origin.x-6-12,42/2.0f, 24/2, 16/2);
            
        }
        
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (tableView == _artistTableView){
        DetailforArtistVC * dvc = [[DetailforArtistVC alloc] init];
        if (isArtist){
            dvc.typeStr = @"1";
            ArtistItem * item = [_artistArray objectAtIndex:indexPath.row];
            dvc.idStr = item.artistId;
        }else{
            dvc.typeStr = @"2";
            GalleryItem * item = [_galleryArray objectAtIndex:indexPath.row];
            dvc.idStr = item.idStr;
            DDLOG(@"ga ====%@ \n type = %@",item.idStr,dvc.typeStr);
        }
        [self.navigationController pushViewController:dvc animated:YES];
    }
    
    if (tableView == _searchTableView){
        [[NSUserDefaults standardUserDefaults] setObject:@"no" forKey:@"SecondViewHiddenSearch"];
        
        SearchItem * item = [_searchDataArray objectAtIndex:indexPath.row];

        DetailforArtistVC * dvc = [[DetailforArtistVC alloc] init];
        dvc.idStr =item.ArtworkId;
        dvc.typeStr = @"1";
        [self.navigationController pushViewController:dvc animated:YES];
        
//        [_searchDataArray removeAllObjects];
//        [searchView.mytextField resignFirstResponder];
//        searchView.mytextField.text = nil;
//        _searchBgview.hidden=YES;
        
        _searchTableView.frame = CGRectMake(0,0, UI_SCREEN_WIDTH, self.contentView.frame.size.height-searchView.frame.size.height-UI_TAB_BAR_HEIGHT);
        [searchView.mytextField resignFirstResponder];
    }
}

#pragma mark ===============UIScrollView Delegate ===========
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _searchTableView){
        _searchTableView.frame = CGRectMake(0,0, UI_SCREEN_WIDTH, self.contentView.frame.size.height-searchView.frame.size.height-UI_TAB_BAR_HEIGHT);
        [searchView.mytextField resignFirstResponder];
    }
}

#pragma mark ===============数据请求======================
-(void)getArtistData
{
    
    //获取艺术家列表
    if ([XDTools NetworkReachable]){
        isHaveMoreData = YES;
        int oldPage = page;
        if (isHeaderRefresh){
            page = 0;
        }else{
            page++;
        }
        NSString * pageStr = [NSString stringWithFormat:@"%d",page];
        NSString *body = [NSString stringWithFormat:@" <GetArtistList xmlns=\"http://tempuri.org/\">"
                          "<pageIndex>%@</pageIndex>"
                          "<limit>20</limit>"
                          "</GetArtistList>",pageStr];
        
        __weak ASIFormDataRequest *request = [XDTools postRequestWithDict:body API:GETARTISTLIST];
        [request setCompletionBlock:^{
            [XDTools hideProgress:self.contentView];
            NSString *responseString = [request responseString];
//            DDLOG(@"responseString:%@", responseString);
            
            int statusCode = [request responseStatusCode];
            NSString *soapAction = [[request requestHeaders] objectForKey:@"SOAPAction"];
            
            NSArray *arraySOAP =[soapAction componentsSeparatedByString:@"/"];
            int count = [arraySOAP count] - 1;
            NSString *methodName = [arraySOAP objectAtIndex:count];
            
            // Use when fetching text data
            NSString *result = nil;
            if (statusCode == 200) {
                //表示正常请求
                result = [SoapXmlParseHelper SoapMessageResultXml:responseString ServiceMethodName:methodName];
                NSDictionary  *responseDict = [XDTools JSonFromString:result];
                if ([[responseDict objectForKey:@"code"] intValue]==0){
                    //请求成功
                    if(page == 0){
                        [_artistArray removeAllObjects];
                    }
                    //[_artistArray removeAllObjects];
                    artistTotalCount = [[responseDict objectForKey:@"totalCount"] intValue];
                    for (NSDictionary * dic in [responseDict objectForKey:@"artists"]){
                        ArtistItem * item = [[ArtistItem alloc] init];
                        item.artistId = [NSString stringWithFormat:@"%@",[dic objectForKey:@"artistId"]];
                        item.artistName = [NSString stringWithFormat:@"%@",[dic objectForKey:@"artistName"]];
                        
                        item.artistOpenTime = [NSString stringWithFormat:@"%@",[dic objectForKey:@"artistOpenTime"]];
                        item.artistPortal = [NSString stringWithFormat:@"%@",[dic objectForKey:@"artistPortal"]];
                        item.artistWorkCount = [NSString stringWithFormat:@"%@",[dic objectForKey:@"artistWorkCount"]];
                        [_artistArray addObject:item];
                    }
                    if (_artistArray.count == 0) {
                        page = oldPage;
                        [XDTools showTips:@"暂无数据" toView:self.contentView];
                    }else{
                        if([[responseDict objectForKey:@"artists"] count]==0){
                            [XDTools showTips:@"没有更多数据" toView:self.contentView];
                            isHaveMoreData =NO;
                        }
                    }
                    
                     [_artistTableView reloadData];
                    
                }else{
                    [XDTools showTips:@"数据获取失败" toView:self.contentView];

                }
                [header endRefreshing];
                [footer endRefreshing];
            }
        }];
        
        [request setFailedBlock:^{
            [XDTools hideProgress:self.contentView];
            [header endRefreshing];
            [footer endRefreshing];
        }];
        
        [request startAsynchronous];
            [XDTools showProgress:self.contentView];
    }else{
        [XDTools showTips:brokenNetwork toView:self.contentView];
    }
   
}

-(void)getGalleryData
{
    
    //获取画廊列表
    if ([XDTools NetworkReachable]){
        isHaveMoreData = YES;
        int oldPage = page2;
        if (isHeaderRefresh){
            page2 = 0;
        }else{
            page2++;
        }
        NSString * pageStr = [NSString stringWithFormat:@"%d",page2];
        NSString *body = [NSString stringWithFormat:@" <GetGalleryList xmlns=\"http://tempuri.org/\">"
                          "<pageIndex>%@</pageIndex>"
                          "<limit>20</limit>"
                          "</GetGalleryList>",pageStr];
        
        __weak ASIFormDataRequest *request = [XDTools postRequestWithDict:body API:GETGALLERYLIST];
        [request setCompletionBlock:^{
            NSString *responseString = [request responseString];
            //            DDLOG(@"responseString:%@", responseString);
            
            int statusCode = [request responseStatusCode];
            NSString *soapAction = [[request requestHeaders] objectForKey:@"SOAPAction"];
            
            NSArray *arraySOAP =[soapAction componentsSeparatedByString:@"/"];
            int count = [arraySOAP count] - 1;
            NSString *methodName = [arraySOAP objectAtIndex:count];
            
            // Use when fetching text data
            NSString *result = nil;
            if (statusCode == 200) {
                //表示正常请求
                result = [SoapXmlParseHelper SoapMessageResultXml:responseString ServiceMethodName:methodName];
                NSDictionary  *responseDict = [XDTools JSonFromString:result];
                if ([[responseDict objectForKey:@"code"] intValue]==0){
                    if(page2 == 0){
                        [_galleryArray removeAllObjects];
                    }
                    //[_galleryArray removeAllObjects];
                    galleryTotalCount = [[responseDict objectForKey:@"totalCount"] intValue];
                    for (NSDictionary * dicc in [responseDict objectForKey:@"galleries"]){
                        GalleryItem * item = [[GalleryItem alloc] init];
                        item.brief = [NSString stringWithFormat:@"%@",[dicc objectForKey:@"brief"]];
                        item.city = [NSString stringWithFormat:@"%@",[dicc objectForKey:@"city"]];
                        item.email = [NSString stringWithFormat:@"%@",[dicc objectForKey:@"email"] ];
                        item.idStr  = [NSString stringWithFormat:@"%@",[dicc objectForKey:@"id"] ];
                        item.logoUrl = [NSString stringWithFormat:@"%@",[dicc objectForKey:@"logoUrl"] ];
                        item.name = [NSString stringWithFormat:@"%@",[dicc objectForKey:@"name"] ];
                        item.pic = [NSString stringWithFormat:@"%@",[dicc objectForKey:@"pic"] ];
                        item.tel = [NSString stringWithFormat:@"%@",[dicc objectForKey:@"tel"] ];
                        item.openTimes = [NSString stringWithFormat:@"%@",[dicc objectForKey:@"openTimes"]];
                        item.workCount = [NSString stringWithFormat:@"%@",[dicc objectForKey:@"workCount"]];
                        
                        [_galleryArray addObject:item];
                    }
                    if (_galleryArray.count == 0) {
                        page2 = oldPage;
                        [XDTools showTips:@"暂无数据" toView:self.contentView];
                    }else{
                        if([[responseDict objectForKey:@"galleries"] count]==0){
                            [XDTools showTips:@"没有更多数据" toView:self.contentView];
                            isHaveMoreData2 =NO;
                        }
                    }
                    
                    [_artistTableView reloadData];

                }else{
                    [XDTools showTips:@"数据获取失败" toView:self.contentView];
                    
                }
                [header endRefreshing];
                [footer endRefreshing];
            }
        }];
        
        [request setFailedBlock:^{
            [header endRefreshing];
            [footer endRefreshing];
        }];
        
        [request startAsynchronous];
    }else{
        [XDTools showTips:brokenNetwork toView:self.contentView];
    }
    
}

-(void)getSearchData:(NSString *)string
{
    if ([XDTools NetworkReachable]){
        NSString *body = [NSString stringWithFormat:@" <SearchPics_2 xmlns=\"http://tempuri.org/\">"
                          "<keyword>%@</keyword>"
                          "<pageidx>0</pageidx>"
                          "<limit>50</limit>"
                          "</SearchPics_2>",string];
        
        __weak ASIFormDataRequest *request = [XDTools postRequestWithDict:body API:SEARCHPICS];
        [request setCompletionBlock:^{
            [XDTools hideProgress:self.contentView];
            //[XDTools hideProgress:self.contentView];
            NSString *responseString = [request responseString];
            // NSDictionary *tempDic = [XDTools  JSonFromString:responseString];
            DDLOG(@"responseString:%@", responseString);
            
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            int statusCode = [request responseStatusCode];
            NSString *soapAction = [[request requestHeaders] objectForKey:@"SOAPAction"];
            
            NSArray *arraySOAP =[soapAction componentsSeparatedByString:@"/"];
            int count = [arraySOAP count] - 1;
            NSString *methodName = [arraySOAP objectAtIndex:count];
            
            // Use when fetching text data
            NSString *result = nil;
            if (statusCode == 200) {
                //表示正常请求
                result = [SoapXmlParseHelper SoapMessageResultXml:responseString ServiceMethodName:methodName];
                NSDictionary  *responseDict = [XDTools JSonFromString:result];
                if ([[responseDict objectForKey:@"code"] intValue]==0){
                    //表示请求成功
                    int curpage = [[responseDict objectForKey:@"curpage"] intValue]; //当前页
                    int tatalPage = [[responseDict objectForKey:@"totalpage"] intValue]; // 总页数
                    [_searchDataArray removeAllObjects];
                    DDLOG(@"9i9i9i9 = %@",responseDict);
                    for (NSDictionary * dic in [responseDict objectForKey:@"nameList"]){
                        SearchItem * item = [[SearchItem alloc] init];
                        item.ArtistName = [NSString stringWithFormat:@"%@",[dic objectForKey:@"ArtistName"]];
                        if ([item.ArtistName isEqualToString:@"<null>"]) {
                            
                            item.ArtistName = @"";
                        }
                        item.ArtworkId = [NSString stringWithFormat:@"%@",[dic objectForKey:@"ArtworkId"]];
                        item.ArtworkName = [NSString stringWithFormat:@"%@",[dic objectForKey:@"ArtworkName"]];
                        if ([item.ArtworkName isEqualToString:@"<null>"]) {
                            item.ArtworkName = @"";
                        }
                        item.color = [NSString stringWithFormat:@"%@",[dic objectForKey:@"color"]];
                        item.picid = [NSString stringWithFormat:@"%@",[dic objectForKey:@"picid"]];
                        item.picurl_s = [NSString stringWithFormat:@"%@",[dic objectForKey:@"picurl_s"]];
                        item.size = [NSString stringWithFormat:@"%@",[dic objectForKey:@"size"]];
                        item.ArtistType = [NSString stringWithFormat:@"%@",[dic objectForKey:@"type"]];
                        if ([item.ArtistType isEqualToString:@"1"]) {
                            [_searchDataArray addObject:item];
                        }
                        
                    }
                    [_searchTableView reloadData];
                    
                }
            }
        }];
        
        [request setFailedBlock:^{
            //            [XDTools hideProgress:self.contentView];
        }];
        
        [request startAsynchronous];
        //        [XDTools showProgress:self.contentView];
    }else{
        [XDTools showTips:brokenNetwork toView:self.contentView];
    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
